<?php

declare(strict_types=1);

namespace App\Model;


use App\Utils\MemcacheClient;

/**
 * @property int $uid
 * @property string $real_name 真实姓名
 * @property string $ID_number 18位身份证号码
 * @property string $ID_photo_1 身份证照片1。正、反、手持三张之一
 * @property string $ID_photo_2 身份证照片2。正、反、手持三张之一
 * @property string $ID_photo_3 身份证照片3。正、反、手持三张之一
 * @property int $ctime 上传时间
 * @property int $status 审核状态。0 待审核  1 审核通过 2 审核不通过
 * @property string $alipay_id 支付宝账号
 * @property string $video_url 小视频
 * @property string $agreement_url 协议图片地址
 * @property string $bank_name 银行名称
 * @property int $bank_code 银行代码
 * @property string $bank_num 银行卡账号
 * @property string $bank_province 开户省/市
 * @property string $bank_city 开户地区
 * @property string $ID_Ext
 * @property int $last_addtime
 * @property int $audio_uid
 * @property string $audio_time
 * @property string $content
 * @property int $is_ignore 是否忽略不审
 * @property int $user_status 用户状态 0正常 1已注销
 * @property int $risk_level 1 – 高危 2 – 嫌疑 3 – 普通 4 – 正常
 */
class UserIdentity extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'tb_user_identity';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['uid' => 'integer', 'ctime' => 'integer', 'status' => 'integer', 'bank_code' => 'integer', 'last_addtime' => 'integer', 'audio_uid' => 'integer', 'is_ignore' => 'integer', 'user_status' => 'integer', 'risk_level' => 'integer'];


    public function getIdentityCache($uid)
    {
        $instance = MemcacheClient::getInstance();
        $cache = $instance->get('real_user_identity_info_' . $uid);
        if ($cache === false) {
            $cache = $this->query()->where('uid', $uid)->first();
            $instance->set('real_user_identity_info_' . $uid, $cache, 300);
        }
        return $cache;
    }

    // 获取用户身份认证信息
    public function getAuthInfo($user, $type): array
    {
        $data = [
            'real_card_status' => $user['is_cardAuth'],
            'real_img_status' => $user['is_faceAuth'] == 3 ? 1 : $user['is_faceAuth'],
            'real_education_status' => 0,
            'is_binding_phone' => $user['is_binding']
        ];

        if ($type == 1) {
            $models = $this->getIdentityCache($user["id"]);
            $data['real_name'] = (!empty($models['real_name'])) ? $models['real_name'] : '';
            $data['real_card_gold'] = AUTH_CARD_COUNT;
            $data['real_img_gold'] = AUTH_FACE_COUNT;
            $data['real_education_gold'] = AUTH_EDUACTION_COUNT;
        }

        return $data;
    }

}
